<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ JSON.stringify(text) == '{}' ? '添加积分发放申请' : '修改积分发放申请' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form :form="form">
					<a-form-item label="名称">
						<a-input v-decorator="['Title', { rules: [{ required: true, message: '请输入名称' }] }]" />
					</a-form-item>
					<a-form-item label="课程类型">
						<a-select :allowClear="true"
							v-decorator="['ActivityTypeID', { rules: [{ required: true, message: '请选择课程类型' }] }]"
							placeholder="全部" optionFilterProp="children" showSearch @change="selectChange">
							<a-select-option v-for="(item, index) in ActivityTypeData" :key="item.ID" :value="item.ID">
								{{ item.Name }}
							</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="级别" v-if="Type=='1'">
						<a-select @change="changClass" :allowClear="true" v-decorator="['ActivityTypeScoreID']"
							placeholder="全部" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in ActivityTypeScoreList" :key="item.ID"
								:value="item.ID">
								{{item.ActivityLevelName}}
							</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="类型" prop="MemberType" v-if="Type=='1'">
						<a-select v-decorator="['MemberType']" style="width:100%">
							<a-select-option v-for="(item, index) in ActivityMemberTypeList" :key="item.Key"
								:value="item.Key">{{ item.Value }}
								({{item.Key=='1'?jibieDetail.JoinScore:item.Key=='2'?jibieDetail.WorkerScore:item.Key=='3'?jibieDetail.AudienceScore:""}}分)
							</a-select-option>
						</a-select>
					</a-form-item>
					
					<a-form-item label="级别"  v-if="Type=='2'">
						<a-select @change="levalChange"  style="width:100%">
							<a-select-option v-for="(item, index) in ActivityTypePrizeTypeList" :key="item.Key"
								:value="item.ID">{{ item.LevelName }}</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="类型" v-if="Type=='2'">
						<a-select v-decorator="['ActivityTypePrizeID']" style="width:100%">
							<a-select-option v-for="(item, index) in leaveChange" :key="item.Key" :value="item.ID">
								{{ item.Title }}({{item.Score}}分)</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="学号">
						<a-textarea placeholder="请输入学号，多个学号之间请换行" :autosize="{ minRows: 10, maxRows: 20 }"
							v-decorator="['userNo', { rules: [{ required: true, message: '请输入学号' }] }]" />
						当前正在录入第{{ form.getFieldValue('userNo') ? form.getFieldValue('userNo').split('\n').length : 0 }}条数据
					</a-form-item>
					<a-form-item label="是否过滤重复" prop="IsFilter">
						<a-radio-group v-decorator="['IsFilter', { rules: [{ required: true, message: '是否过滤重复' }] }]" name="radioGroup">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-item>
					<a-form-item label="参加/获奖时间" prop="GetTime">
						<a-date-picker show-time v-decorator="['GetTimeData']" format="YYYY-MM-DD" />
					</a-form-item>
					
					<!-- 多个 -->
					<a-form-model-item label="附件">
						<a-upload :action="requestUrl+'Common/DoUploadFile?type=file'" :file-list="icofileList"
						:default-file-list="defaultFileList"
							@change="e => icohandleChange(e, 'Ico')">
							<div>
								<!-- <a-icon type="plus" /> -->
								<!-- <div class="ant-upload-text">Upload</div> -->
								<a-button>
									<a-icon type="upload" /> 上传
								</a-button>
							</div>
						</a-upload>
					</a-form-model-item>
					<a-form-item label="备注">
						<a-textarea placeholder="请输入备注" :autosize="{ minRows: 3, maxRows: 10 }"
							v-decorator="['Remark']" />
					</a-form-item>
					<a-form-item class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">
							{{ JSON.stringify(text) == '{}' ? '确认添加' : '确认修改' }}
						</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-item>
				</a-form>
			</div>
		</div>
	</div>
</template>

<script>
	import moment from 'moment';
	import {
		ManageCreditSendRecordDoAdd,
		ManageCreditFlowTypeDoUpdate,
		CommonDoGetActivityTypeScoreList,
		GetActivityTypePrizeListCommon,
		ManageCreditApplyDoGetDetail
	} from '@/api/follow';
	export default {
		props: {
			text: Object,
			ActivityTypeData: Array,
			ActivityTypePrizeTypeList: Array,
			ActivityMemberTypeList: Array
		},
		watch: {
			text: function(text) {
				let that = this;
				if (JSON.stringify(text) == '{}') {
					that.currentID = ''
					// setTimeout(function() {
					// 	that.form.setFieldsValue({
					// 		IsTeacherExam: false,
					// 		IsAgreeUpdate: false
					// 	});
					// },300)
				} else {
					that.currentID = text.ID;
					that.getDetail(text.ID)
					// text.GetTimeData = [that.moment(text.ConfirmStartTime)]
					console.log(text);
					setTimeout(function() {
						that.form.setFieldsValue({
							Title: text.Title,
							ActivityTypeID: text.ActivityTypeID,
							ActivityTypeScoreID: text.ActivityTypeScoreID,
							ActivityTypePrizeID: text.ActivityTypePrizeID,
							MemberType: text.MemberType,
							GetTimeData: text.GetTimeData,
						});
						// for (let i in text) {
						// 	that.form.setFieldsValue({
						// 		[i]: text[i]
						// 	});
						// }
					}, 300);
				}
			}
		},
		data() {
			return {
				Type:'',
					defaultFileList:[],
				requestUrl: this.global.requestUrl,
				icofileList: [],
				fileList: {},
				showstate: true,
				// ActivityTypePrizeTypeList:[],
				GetActivityTypePrizeListCommon: [],
				ActivityTypeScoreList: [],
				dataForm: [],
				loading: false,
				formLayout: 'horizontal',
				form: this.$form.createForm(this, {
					name: 'coordinated'
				}),
				currentID: '',
				UserList: [],
				leaveChange:[],
				jibieDetail:{},
			};
		},
		created() {},
		methods: {
			levalChange(e) {
				this.leaveChange = this.ActivityTypePrizeTypeList.filter((m, i) => m.ID == e)
			},
			changClass(e) {
				this.jibieDetail = this.ActivityTypeScoreList.filter((m, i) => m.ID == e)[0]
			},
			icohandleChange({
				fileList
			}, name) {
				this.icofileList = fileList;
			},
			async getDetail(id) {
				let data = {};
				data.id = id
				let res = await ManageCreditApplyDoGetDetail(data);
				this.DetailData = res.data.data;
				let that = this;
				let text = res.data.data;
				console.log(this.DetailData);
				that.dataForm = res.data.formData;
				setTimeout(function() {
					// that.form.setFieldsValue({
					// 	Title: text.Title,
					// 	ActivityTypeID: text.ActivityTypeID,
					// 	ActivityTypeScoreID: text.ActivityTypeScoreID,
					// 	ActivityTypePrizeID: text.ActivityTypePrizeID,
					// 	MemberType: text.MemberType,
					// 	GetTimeData:text.GetTimeData,
					// });
					// for (let i in text) {
					// 	that.form.setFieldsValue({
					// 		[i]: text[i]
					// 	});
					// }
				}, 300);
			},
			async getScore(key) {
				let data = {};
				data.activityTypeID = key
				let res4 = await CommonDoGetActivityTypeScoreList(data);
				this.ActivityTypeScoreList = res4.data.data;
				// let data1={}
				// data1.activityID=key;
				// let res=await GetActivityTypePrizeListCommon(data1);
				// this.GetActivityTypePrizeListCommon=res.data.data;

			},
			selectChange(e) {
				this.getScore(e);
			},
			moment,
			closeFunction(data) {
				this.form.resetFields();
				this.$emit('closeFun', data);
			},
			isAssetTypeAnImage(ext) {
				return ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].
				indexOf(ext.toLowerCase()) !== -1;
			},
			handleSubmit(e) {
				this.form.validateFields(async (err, values) => {
					if (!err) {
						let data = {};
						data = values;
						let arr1 = [];
						for (let i = 0; i < this.icofileList.length; i++) {
							let name = this.icofileList[i].response.fileName;
							let index = name.lastIndexOf(".");
							let ext = name.substr(index + 1);
							let type = ''
							if (this.isAssetTypeAnImage(ext)) {
								type = 1
							} else {
								type = 2
							}
							arr1.push(this.icofileList[i].response.fileName + '|' + this.icofileList[i]
								.response.url + '|' + type)
						}
						data.attachList = arr1.join('_')
						if(data.GetTimeData!=''){
							data.GetTime = moment(data.GetTimeData._d).format('YYYY-MM-DD');
						}
						// for (let m = 0; m < this.icofileList.length; m++) {
						// 	if (this.icofileList[m].url) {
						// 		if (this.icofileList[m].url.indexOf(this.requestUrl) > -1) {
						// 			this.icofileList[m].url = this.icofileList[m].url.substring(this.requestUrl.length);
						// 		}
						// 		arr1.push(this.icofileList[m].url);
						// 	} else {
						// 		arr1.push(this.icofileList[m].response.url);
						// 	}
						// }
						// data.FilePath = arr1.join(',');
						// data.FileName = values.FileName;
						data.fileData=[{"FileName":"121","FilePath":"1111"}],
						data.SplitChar='\n';
						console.log(data);
						// return;
						let res = {};
						if (!this.currentID) {
							res = await ManageCreditSendRecordDoAdd(data);
						} else {
							data.id = this.currentID;
							res = await ManageCreditFlowTypeDoUpdate(data);
						}
						if (res.data.code === 0) {
							this.$message.success(res.data.msg);
							this.closeFunction('1');
						} else {
							this.$message.error(res.data.msg);
						}
					}
				});
			}
		}
	};
</script>

<style lang="less" scoped>
	/deep/.ant-form {
		margin-top: 50px;
		padding-bottom: 50px;
	}

	/deep/.ant-form-item {
		margin: 20px 50px;
		display: flex;
	}

	/deep/.ant-form-item-label {
		width: 100px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	/deep/.ant-calendar-picker {
		width: 100% !important;
	}

	.ant-upload-list-picture-card-container {
		width: 80px;
		height: 80px;
	}
</style>
